<template>
  <Dialog :title="dialogTitle" v-model="dialogVisible">
    <el-form
      ref="formRef"
      :model="formData"
      :rules="formRules"
      label-width="100px"
      v-loading="formLoading"
    >
      <el-form-item label="客户编号" prop="purchaseOrderNo">
        <el-input v-model="formData.purchaseOrderNo" placeholder="请输入采购单号码" />
      </el-form-item>
      <el-form-item label="客户名称" prop="purchaseOrderNo">
        <el-input v-model="formData.purchaseOrderNo" placeholder="请输入采购单号码" />
      </el-form-item>
      <el-form-item label="供应商" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="生产单号" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="货号" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="原料长" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="原料宽" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="厚度" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="材质名称" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="品名" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入供应商" />
      </el-form-item>
      <el-form-item label="订单日期" prop="orderDate">
        <el-date-picker
          v-model="formData.orderDate"
          type="daterange"
          placeholder="选择订单日期"
        />
      </el-form-item>
      <el-form-item label="材质类型" prop="purchaseType">
        <el-select v-model="formData.purchaseType" placeholder="请选择采购类型">
          <el-option label="请选择" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="采购状态" prop="purchaseType">
        <el-select v-model="formData.purchaseType" placeholder="请选择采购类型">
          <el-option label="请选择" value="" />
        </el-select>
      </el-form-item>
      <el-form-item label="合订长" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入合订长" />
      </el-form-item>
      <el-form-item label="合订宽" prop="supplierId">
        <el-input v-model="formData.supplierId" placeholder="请输入合订宽" />
      </el-form-item>
      <el-form-item label="自动匹配安全库存标签" label-width="200" prop="supplierId">
        <el-checkbox v-model="formData.supplierId" />
      </el-form-item>
      <el-form-item label="显示已采购完毕" label-width="200" prop="supplierId">
        <el-checkbox v-model="formData.supplierId" />
      </el-form-item>
      <el-form-item>
        <el-button @click="getList" type="primary"><Icon icon="ep:search" class="mr-5px" /> 搜索</el-button>
        <el-button @click="resetForm"><Icon icon="ep:refresh" class="mr-5px" /> 重置</el-button>
      </el-form-item>
    </el-form>
    <!-- 列表 -->
  <ContentWrap>
    <el-tabs
      v-model="tabSelected"
    >
      <el-tab-pane label="明细" name="detail">
        <ReusableTable
          :tableData="list"
          :columns="columnsdetail"
          :loading="formLoading"
          @selection-change="handleSelectionChange"
        />
      </el-tab-pane>
      <el-tab-pane label="配置" name="config">
        对应状态 前景色
      </el-tab-pane>
      <el-tab-pane label="汇总" name="overall">
        <ReusableTable
          :tableData="list"
          :columns="columnsoverall"
          :loading="formLoading"
          @selection-change="handleSelectionChange"
        />
      </el-tab-pane>
    </el-tabs>
  </ContentWrap>
    <template #footer>
      <el-button @click="submitForm" type="primary" :disabled="formLoading">确 定</el-button>
      <el-button @click="dialogVisible = false">取 消</el-button>
    </template>
  </Dialog>
</template>
<script setup lang="ts">
import { PurRawMaterialPurchaseOrderApi, PurRawMaterialPurchaseOrderVO } from '@/api/erp/purrawmaterialpurchaseorder'

/** ERP采购管理-原料采购单主 表单 */
defineOptions({ name: 'PurRawMaterialPurchaseOrderForm' })

const { t } = useI18n() // 国际化
const message = useMessage() // 消息弹窗

const emit = defineEmits(['confirm', 'cancel']);

const dialogVisible = ref(false) // 弹窗的是否展示
const dialogTitle = ref('') // 弹窗的标题
const formLoading = ref(false) // 表单的加载中：1）修改时的数据加载；2）提交的按钮禁用
const formType = ref('') // 表单的类型：create - 新增；update - 修改

const formData = ref({
  id: undefined,
  purchaseOrderNo: undefined,
  supplierId: undefined,
  supplierName: undefined,
  purchaseType: undefined,
  orderDate: undefined,
})

const tabSelected = ref('detail')

/** table表头column */
const columnsdetail = ref([
  { label: '生产单号', prop: 'supplierName' },
  { label: '订货日期', prop: 'supplierName', type: 'date' },
  { label: '客户交货期', prop: 'supplierName', type: 'date' },
  { label: '订单号', prop: 'supplierName' },
  { label: '货号', prop: 'supplierName' },
  { label: '客户', prop: 'supplierName' },
  { label: '纸质', prop: 'supplierName' },
  { label: '需求数', prop: 'supplierName' },
  { label: '订单规格', prop: 'supplierName' },
  { label: '宽', prop: 'supplierName' },
  { label: '长', prop: 'supplierName' },
  { label: '品名', prop: 'supplierName' },
  { label: '箱型', prop: 'supplierName' },
  { label: '净板纸宽', prop: 'supplierName' },
  { label: '净板纸长', prop: 'supplierName' },
  { label: '净板需求数', prop: 'supplierName', width: 120 },
  { label: '订单面积', prop: 'supplierName' },
  { label: '备注', prop: 'supplierName' },
  { label: '供应商名称', prop: 'supplierName' },
  { label: '厚度', prop: 'supplierName' },
  { label: '停止采购', prop: 'supplierName' },
  { label: '用料类型', prop: 'supplierName' },
  { label: '材质编号', prop: 'supplierName' },
  { label: '压线类型', prop: 'supplierName' },
  { label: '横压1', prop: 'supplierName' },
  { label: '横压2', prop: 'supplierName' },
  { label: '横压3', prop: 'supplierName' },
  { label: '楞型', prop: 'supplierName' },
  { label: '计算数量', prop: 'supplierName' },
  { label: '宽开', prop: 'supplierName' },
  { label: '长开', prop: 'supplierName' },
  { label: '库存数', prop: 'supplierName' },
  { label: '体积', prop: 'supplierName' },
  { label: '品名2', prop: 'supplierName' },
  { label: '生产单数量', prop: 'supplierName', width: 120 },
  { label: '模数', prop: 'supplierName' },
  { label: '模长', prop: 'supplierName' },
  { label: '模宽', prop: 'supplierName' },
  { label: '横压9', prop: 'supplierName' },
  { label: '横压10', prop: 'supplierName' },
  { label: '内订单号', prop: 'supplierName' },
  { label: '用料备注1', prop: 'supplierName', width: 120 },
  { label: '用料备注2', prop: 'supplierName', width: 120 },
  { label: '用料备注3', prop: 'supplierName', width: 120 },
  { label: '用料备注4', prop: 'supplierName', width: 120 },
  { label: '用料备注5', prop: 'supplierName', width: 120 },
  { label: '用料备注6', prop: 'supplierName', width: 120 },
])

const columnsoverall = ref([
  { label: '材质编号', prop: 'supplierName' },
  { label: '材质名称', prop: 'supplierName' },
  { label: '宽', prop: 'supplierName' },
  { label: '长', prop: 'supplierName' },
  { label: '总长', prop: 'supplierName' },
  { label: '总面积', prop: 'supplierName' },
  { label: '总数量', prop: 'supplierName' },
  { label: '总重量', prop: 'supplierName' },
  { label: '压线类型', prop: 'supplierName' },
  { label: '横压1', prop: 'supplierName' },
  { label: '横压2', prop: 'supplierName' },
  { label: '横压3', prop: 'supplierName' },
  { label: '横压4', prop: 'supplierName' },
  { label: '横压5', prop: 'supplierName' },
])

const list = ref([])

/** 查询列表 */
const getList = async () => {
  formLoading.value = true
  try {
    const data = await PurRawMaterialPurchaseOrderApi.getPurRawMaterialPurchaseOrderPage(formRef)
    list.value = data.list
    // total.value = data.total
  } finally {
    formLoading.value = false
  }
}

const formRules = reactive({
})
const formRef = ref() // 表单 Ref

/** 打开弹窗 */
const open = async (type: string, id?: number) => {
  dialogVisible.value = true
  dialogTitle.value = t('action.' + type)
  formType.value = type
  resetForm()
  // 修改时，设置数据
  if (id) {
    formLoading.value = true
    try {
      formData.value = await PurRawMaterialPurchaseOrderApi.getPurRawMaterialPurchaseOrder(id)
    } finally {
      formLoading.value = false
    }
  }
}
defineExpose({ open }) // 提供 open 方法，用于打开弹窗

const handleSelectionChange = () => {}

/** 提交表单 */
const submitForm = async () => {
  // 校验表单
  await formRef.value.validate()
  // 提交请求
  formLoading.value = true
  try {
    const data = formData.value as unknown as PurRawMaterialPurchaseOrderVO
    if (formType.value === 'create') {
      await PurRawMaterialPurchaseOrderApi.createPurRawMaterialPurchaseOrder(data)
      message.success(t('common.createSuccess'))
    } else {
      await PurRawMaterialPurchaseOrderApi.updatePurRawMaterialPurchaseOrder(data)
      message.success(t('common.updateSuccess'))
    }
    dialogVisible.value = false
    // 发送操作成功的事件
    emit('confirm')
  } finally {
    formLoading.value = false
  }
}

/** 重置表单 */
const resetForm = () => {
  formData.value = {
    id: undefined,
    purchaseOrderNo: undefined,
    supplierId: undefined,
    supplierName: undefined,
    purchaseType: undefined,
    approver: undefined,
    approveTime: undefined,
    recalculateWidth: undefined,
    orderDate: undefined,
    materialType: undefined,
    isAccounted: undefined,
    currency: undefined,
    exchangeRate: undefined,
    quotationUnit: undefined,
    deliveryDate: undefined,
    basicAmount: undefined,
    discount: undefined,
    discountedAmount: undefined,
    taxType: undefined,
    taxRate: undefined,
    discountAmount: undefined,
    taxAmount: undefined,
    totalWeight: undefined,
    totalArea: undefined,
    totalVolume: undefined,
    deliveryDuration: undefined,
    totalAmount: undefined
  }
  formRef.value?.resetFields()
}
</script>